﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleRobot
{
    class Program
    {        
        static void Main()
        {
            Random rnd = new Random();
            GameRules gr1 = new GameRules(30, 19);
            int xmin = gr1._xmin;
            int ymin = gr1._ymin;
            int xmax = gr1._xmax;
            int ymax = gr1._ymax;
            Console.CursorVisible = false;
            SetGameFrame("@", "@", ConsoleColor.DarkGray, gr1);
            Robot r1 = new Robot(xmin + 1, ymin + 1, ConsoleColor.Red, gr1); r1.Print();
            Robot r2 = new Robot(xmin + 1, ymax - 1, ConsoleColor.Yellow, gr1); r2.Print();
            Robot r3 = new Robot(xmax - 1, ymin + 1, ConsoleColor.Green, gr1); r3.Print();
            Robot r4 = new Robot(xmax - 1, ymax - 1, ConsoleColor.Blue, gr1); r4.Print();
            PrintStartMenu();
            SetGameFrame("@", "@", ConsoleColor.DarkGray, gr1);
            int[] coordTarg=SetRandomTarget("#", ConsoleColor.Red, ConsoleColor.White, rnd, gr1);
            int xtg = coordTarg[0]; int ytg = coordTarg[1];
            int interval = 1;
            ConsoleColor winner = ConsoleColor.Black;
            do
            {
                Thread.Sleep(50);
                r1.MakeRandomStep(interval, gr1, rnd); r1.Print();
                if (r1._x == xtg & r1._y == ytg) { winner = r1._color; break; }                
                r2.MakeRandomStep(interval, gr1, rnd); r2.Print();
                if (r2._x == xtg & r2._y == ytg) { winner = r2._color; break; }
                r3.MakeRandomStep(interval, gr1, rnd); r3.Print();
                if (r3._x == xtg & r3._y == ytg) { winner = r3._color; break; }
                r4.MakeRandomStep(interval, gr1, rnd); r4.Print();
                if (r4._x == xtg & r4._y == ytg) { winner = r4._color; break; }
            } while(true);
            Console.SetCursorPosition(xmax / 2 - 8, ymax / 2); Console.ForegroundColor = winner;
            Console.Write("{0} ROBOT Wins!",winner.ToString());
            Console.ReadKey();
            /*
              8. Добавить в движения роботов интелекта неповторяемости движений
                          */
        }
        static void SetGameFrame(string horElement, string vertElement, ConsoleColor frameColor,GameRules rules)
        {            
            int xmin = rules._xmin;
            int ymin = rules._ymin;
            int xmax = rules._xmax;
            int ymax = rules._ymax;
            Console.SetWindowSize(1, 1);
            Console.ForegroundColor=frameColor;
            for (int x = xmin; x <= xmax; x++)
            {
                Console.SetCursorPosition(x, ymin); Console.Write(horElement);
                Console.SetCursorPosition(x, ymax); Console.Write(horElement);                
            }
            Console.SetWindowSize(10, 6);
            for (int y = ymin; y <= ymax; y++)
            {
                Console.SetCursorPosition(xmin, y); Console.WriteLine(vertElement);
                Console.SetCursorPosition(xmax, y); Console.WriteLine(vertElement);
            }            
            Console.ResetColor();            
            Console.SetWindowSize(xmax+1, ymax+1);
            Console.SetCursorPosition(xmin, ymin);
        }
        static int[] SetRandomTarget(string targetName, ConsoleColor textColor,ConsoleColor backColor, Random rnd, GameRules rules)
        {
            int xAdd = targetName.Length;
            int xmin = rules._xmin;
            int ymin = rules._ymin;
            int xmax = rules._xmax-xAdd;
            int ymax = rules._ymax;
            int x = rnd.Next(xmin, xmax);
            int y = rnd.Next(ymin, ymax);
            Console.SetCursorPosition(x, y);
            Console.BackgroundColor = backColor; Console.ForegroundColor = textColor;
            Console.Write(targetName); Console.ResetColor();
            int[] coordTarg={x,y};
            return coordTarg; 
        }
        static void PrintStartMenu()
        {           
            int ystart = 5;
            int xstart = 5;
            Console.SetCursorPosition(xstart+16, ystart);
            Console.BackgroundColor = ConsoleColor.DarkYellow; Console.ForegroundColor = ConsoleColor.Black;
            Console.SetWindowSize(60, 25);
            Console.WriteLine(" FIRST ROBOT WINS ");
            Console.ResetColor(); Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.SetCursorPosition(xstart, ystart + 1);
            Console.WriteLine("                                                ");
            Console.SetCursorPosition(xstart, ystart + 2);
            Console.WriteLine(" In a moment You'll find a target, which appears");
            Console.SetCursorPosition(xstart, ystart + 3);
            Console.WriteLine(" casually on the game field.  Any of robots from");
            Console.SetCursorPosition(xstart, ystart + 4);
            Console.WriteLine(" the corners can make 1 step in a random course.");
            Console.SetCursorPosition(xstart, ystart + 5);
            Console.WriteLine(" The first one who reaches a target, wins.");
            Console.SetCursorPosition(xstart, ystart + 6);
            Console.WriteLine(" Can you predict who will be the first?");
            Console.SetCursorPosition(xstart, ystart + 8);
            Console.WriteLine(" To Start press any key...");
            Console.SetCursorPosition(0, 0); Console.ReadKey();
            Console.ResetColor(); Console.Clear();
        }
    }
}
